#!/bin/bash
# Author: PortWINE-Linux.ru
########################################################################
[ $(id -u) -eq 0 ] && echo "Do not run this script as root!" && zenity --error --text "Do not run this script as root!" && exit 1
########################################################################
sszen() {
zenity --progress --title="Settings..." --text="Updating start parameters" --pulsate --auto-close --auto-kill --width=450
} 
########################################################################
pwzen() {
zenity --progress --title="Settings..." --text="Please wait!" --pulsate --auto-close --auto-kill --width=450
} 
########################################################################
export kill_winedevice=0
export kill_explorer=0
########################################################################
cd "$(dirname "`readlink -f "$0"`")"
export link="$(pwd)"
cd "${link}/../../"
export PORT_WINE_PATH="$(pwd)"
cd "${link}"
. "${link}"/vars
export config_path="${PORT_WINE_PATH}/data/tmp"
if [ ! -d "${config_path}" ]
then
	mkdir -p "${config_path}"
fi
if [ ! -e "${config_path}/${portname}_loc" ]
then
	SET_LANG=`zenity --title  "Install $portname" --text "Select the language\nВыберите язык" --list --radiolist \
	--column="Set (Выбор)" --column "Language (Язык):" \
	TRUE "RUS" \
	FALSE "ENG" `
	echo "${SET_LANG}" > "${config_path}/${portname}_loc"
fi
. "${link}"/lang
if [ ! -e "${config_path}/${portname}_ver" ]
then
	echo "10" > "${config_path}/${portname}_ver"
fi
if [ ! -f "${config_path}/dxvk_on" ]
then
    start_settings=`zenity --title  "${ss_title}" --text "${ss_text}" --list --radiolist \
    --column="${inst_set}" --column "${ss_ver}" --column "${ss_dr}"  --width=600 --height=250 \
    FALSE "OpenGL" "${ss_ogl_2}" \
    FALSE "DXVK+HUD" "${ss_ogl_3} + HUD" \
    TRUE "DXVK" "${ss_ogl_3}" `
    if [ $? -eq 1 ];then exit 1; fi
    case $start_settings in
    "OpenGL") 
            echo "0" > "${config_path}/dxvk_on" ;;
    "DXVK") 
            echo "1" > "${config_path}/dxvk_on" ;;
    "DXVK+HUD") 
            echo "2" > "${config_path}/dxvk_on" ;;
    esac
fi
read "var_dxvk_on" < "${config_path}/dxvk_on"
export "var_dxvk_on"
if [ -x "`which "gamemoderun" 2>/dev/null`" ]
then
    export var_gamemode=gamemoderun
else
    export var_gamemode="env"
    echo "Gamemod not installed!"
fi
########################################################################
WINEDIR="${PORT_WINE_PATH}"/data/dist
WINELIB="${PORT_WINE_PATH}"/data/libs
export PROTONRUN="${PORT_WINE_PATH}/data/proton"
export WINEARCH=win64
export WINELOADER="${WINEDIR}/bin/wine"
export WINEDLLPATH="${WINEDIR}/lib64/wine:${WINEDIR}/lib/wine"
export WINESERVER="${WINEDIR}/bin/wineserver"
export WINEBOOT="${WINEDIR}/bin/wineboot" 
export WINEPREFIX="${PORT_WINE_PATH}/data/pfx"
export PATH="${WINEDIR}/bin":${PATH}
export WINESTART="C:\\windows\\command\\start.exe"
export STEAM_COMPAT_DATA_PATH="${PORT_WINE_PATH}/data/"
if [ -d "${WINELIB}" ]
then
    export LD_LIBRARY_PATH="${WINEDIR}/lib64/wine:${WINEDIR}/lib/wine:${WINELIB}/pinned_libs_32:${WINELIB}/pinned_libs_64:/usr/lib/x86_64-linux-gnu/libfakeroot:/lib/i386-linux-gnu:/usr/lib/i386-linux-gnu:/usr/local/lib:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/lib:/usr/lib:/usr/lib/i386-linux-gnu/i686:/usr/lib/i386-linux-gnu/sse2:/usr/lib/i386-linux-gnu/i686/sse2:${WINELIB}/i386/lib/i386-linux-gnu:${WINELIB}/i386/lib:${WINELIB}/i386/usr/lib/i386-linux-gnu:${WINELIB}/i386/usr/lib:${WINELIB}/amd64/lib/x86_64-linux-gnu:${WINELIB}/amd64/lib:${WINELIB}/amd64/usr/lib/x86_64-linux-gnu:${WINELIB}/amd64/usr/lib:${WINELIB}/usr/lib/i386-linux-gnu:${WINELIB}/usr/lib/x86_64-linux-gnu:${WINELIB}/amd64/usr/lib:${WINELIB}/lib/i386-linux-gnu:${WINELIB}/lib/x86_64-linux-gnu:${WINELIB}/lib:${LD_LIBRARY_PATH}"
    echo "runtime libs is enabled"
else
    export LD_LIBRARY_PATH="${WINEDIR}/lib64:${WINEDIR}/lib:${LD_LIBRARY_PATH}"
    echo "runtime libs is disabled"
fi
########################################################################
export DXVK_STATE_CACHE_PATH="${PATH_TO_GAME}"
export DXVK_STATE_CACHE=1

export urlg="http://portwine-linux.ru/donate"
########################################################################
START_PORTWINE ()
{
"${WINESERVER}" -k
sh "${link}"/port_update 
export PROTON_NO_FSYNC=0
if [ -x "`which nvidia-settings 2>/dev/null`" ]
then
    nvidia-settings -a [gpu:0]/GPUPowerMizerMode=1 > /dev/null
    export VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/nvidia_icd.json
    export __GL_SYNC_TO_VBLANK=1
    export __GL_SHADER_DISK_CACHE_PATH="${PATH_TO_GAME}" 
    export __GL_SHADER_DISK_CACHE=1
    export __GL_SHADER_DISK_CACHE_SIZE=1000000000
    export __GL_THREADED_OPTIMIZATIONS=1
    export __NV_PRIME_RENDER_OFFLOAD=1
    export __GLX_VENDOR_LIBRARY_NAME=nvidia
else
    export DRI_PRIME=1
    export vblank_mode=1
    export mesa_glthread=true
    local AMD_ATI="$(lspci | grep AMD/ATI)" 
    if [ ! -z "${AMD_ATI}" ] && [ "${PORTWINE_ACO}" -eq "1" ] 
    then
        export RADV_PERFTEST=aco
        echo "ACO is enabled"
    else
        echo "ACO is disabled"
    fi
fi
if [ "${var_dxvk_on}" -eq "1" ] 
then
    export DXVK_HUD=0
elif [ "${var_dxvk_on}" -eq "2" ] 
then
    export DXVK_HUD="fps,devinfo,version,frametimes"
fi
export DXVK_CONFIG_FILE="${PORT_WINE_PATH}/data/dxvk.conf"
export def_pfx="${PORT_WINE_PATH}data/dist/share/default_pfx/"
if [ ! -d "${def_pfx}" ]
then
	"${PROTONRUN}" "run" | pwzen
fi
ADD_IN_START_PORTWINE
} 
########################################################################
WAIT_WINESERVER ()
{
sleep 3
while [ $(pgrep wineserver)>'0' ]
do
	sleep 2
done
}
########################################################################
KILL9_WINEDEVICE ()
{
if [ "${kill_winedevice}" -eq "1" ] 
then
    sleep 10
    killall -r -s9 winedevice 
fi
if [ "${kill_explorer}" -eq "1" ] 
then
    sleep 10
    killall -r -s9 explorer
fi
}
########################################################################
STOP_PORTWINE ()
{
WAIT_WINESERVER
ADD_IN_STOP_PORTWINE
sleep 3
#killall -r -s9 winedevice 
rm -f "${PORT_WINE_PATH}"/0
rm -f "${link}"/0
}
